package com.atguigu.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.hosp.repository.DepartmentRepository;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;
import com.atguigu.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentRepository departmentRepository;

    //保存科室
    @Override
    public void save(Map<String, Object> paramMap) {
        //1.封装参数数据
        //把map转换成json字符串
        String paramMapString = JSONObject.toJSONString(paramMap);
        Department department = JSONObject.parseObject(paramMapString,Department.class);
        //根据医院编码 科室编码查询科室信息
        Department departmentExist =
            departmentRepository.getByHoscodeAndDepcode(department.getHoscode(),department.getDepcode());
        //判断是更新还是新增
        if (departmentExist != null){
            department.setId(departmentExist.getId());
            department.setCreateTime(departmentExist.getCreateTime());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }else {
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }

    }

    //根据查询条件 分页查询科室信息
    @Override
    public Page<Department> selectPage(int page, int limit, DepartmentQueryVo departmentQueryVo) {
        //创建分页对象pageable
        //1.排序
        Sort sort = Sort.by(Sort.Direction.DESC,"createTime");
        //1.2分页对象 0为第一页
        Pageable pageable = PageRequest.of(page-1,limit,sort);
        //2 创建查询条件对象example
        //2.1创建查询条件对象
        Department department = new Department();
        BeanUtils.copyProperties(departmentQueryVo,department);
        department.setIsDeleted(0);
        //2.2创建匹配器 如何使用查询条件
        ExampleMatcher matcher = ExampleMatcher.matching()//构建对象
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)//模糊查询设置
                .withIgnoreCase(true);//忽略大小写

        //创建实例
        Example<Department> example = Example.of(department,matcher);
        Page<Department> pages = departmentRepository.findAll(example,pageable);

        return pages;
    }
    //根据医院编码 科室编码删除科室
    @Override
    public void remove(String hoscode, String depcode) {
        //根据医院编码 科室编码查询科室信息
        Department department =
        departmentRepository.getByHoscodeAndDepcode(hoscode,depcode);
        //根据id删除
        if (department!=null){
            departmentRepository.deleteById(department.getId());
        }

    }
    //根据医院编号，查询医院所有科室列表
    @Override
    public List<DepartmentVo> findDeptTree(String hoscode) {
        //1创建最终返回对象
        List<DepartmentVo> result = new ArrayList<>();
        //2根据医院编号查询所有科室
        //2.1创建查询参数对象
        Department  department = new Department();
        department.setHoscode(hoscode);
        //2.2创建查询模板
        Example<Department> example = Example.of(department);
        //2.3查询数据
        List<Department> departmentList = departmentRepository.findAll(example);

        //3处理科室集合，根据大科室id进行划分，用流式计算
        //map 分析：key是bigcode，value是当前大科室下小科室集合

        Map<String,List<Department>> departmentMap =
                departmentList.stream().collect(
                        Collectors.groupingBy(Department::getBigcode));

        //4 遍历map，封装大科室信息
        for (Map.Entry<String, List<Department>> entry : departmentMap.entrySet()) {
            DepartmentVo bigDeptVo = new DepartmentVo();
            bigDeptVo.setDepcode(entry.getKey());
            bigDeptVo.setDepname(entry.getValue().get(0).getBigname());
            //5 封装小科室信息
            //5.1创建封装小科室集合
            List<DepartmentVo> deptVoList = new ArrayList<>();
            //5.2获取小科室集合查询结果
            List<Department> deptList = entry.getValue();
            //5.3 遍历deptList，获取数据封装deptVoList
            for (Department dept : deptList) {
                DepartmentVo departmentVo = new DepartmentVo();
                BeanUtils.copyProperties(dept,departmentVo);
                deptVoList.add(departmentVo);
            }
            //6 小科室集合存入大科室对象
            bigDeptVo.setChildren(deptVoList);
            //7 大科室对象存入返回的集合
            result.add(bigDeptVo);
        }
        return result;
    }

    @Override
    public String getDepName(String hoscode, String depcode) {
        //根据医院编码 科室编码查询科室信息
        Department department =
                departmentRepository.getByHoscodeAndDepcode(hoscode,depcode);
        String depname = "";
        if (department != null){
            depname = department.getDepname();
        }
        return depname;
    }

    //查询科室
    @Override
    public Department getDepartment(String hoscode, String depcode) {
        //根据医院编码 科室编码查询科室信息
        Department department =
                departmentRepository.getByHoscodeAndDepcode(hoscode,depcode);
        return department;
    }


}
